<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Type;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Agp\CoreBundle\Type\TimeStamp;
use Doctrine\DBAL\Types\ConversionException;

/**
 * Description of TimeStampType
 *
 * @author bondcs
 * 
 * // A ADIÇÃO DE TYPE ESTA NO BOOT DO AgpAgapeBundle.php
 */
class TimestampType extends Type
{   
    const TIMESTAMP = 'timestamp';
    
    public function getName()
    {
        return self::TIMESTAMP;
    }

    /**
     * {@inheritdoc}
     */
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return "TIMESTAMP";
    }

    /**
     * {@inheritdoc}
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return ($value !== null)
            ? $value->getTimestamp() : null;
    }

    /**
     * {@inheritdoc}
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        if ($value === null || $value instanceof \DateTime) {
            return $value;
        }
        
        $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
        if ( ! $val) {
            throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getTimeFormatString());
        }
        
        return $val;
    }
  
}

?>
